package cn.net.xyan.easy.spring.utils;

import cn.net.xyan.easy.spring.annotation.processor.RestServiceProcessor;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;

/**
 * Created by zarra on 16/3/8.
 */
public class TemplateUtils {
    static public Configuration configuration(){
        Configuration cfg = new Configuration();
        cfg.setClassForTemplateLoading(RestServiceProcessor.class,"/");
        cfg.setDefaultEncoding("UTF-8");
        return cfg;
    }

    static public void render(String template, Object root, OutputStream outputStream) throws IOException, TemplateException {
        Configuration cfg = configuration();
        Template temp = cfg.getTemplate(template);

        Writer out = new OutputStreamWriter(outputStream, Charset.forName("UTF-8"));

        temp.process(root, out);

        out.flush();

        outputStream.flush();

    }
}
